<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="no.ntnu.tdt4237.*"%>
<%@ page import="no.ntnu.tdt4237.helperactions.*"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<tags:header title=" - Profile"></tags:header>

<script language="JavaScript" type="text/JavaScript">
	function redirect() {
		var loc = location.href.split(':');
		if (loc[0]=='http') {
			location.href='https:'+loc[1].substring(0,loc[1].length-1)+":8453";
		}
	}
</script>

<body onload="redirect()">
	<%


	
	User loggedInUser = (User) session.getAttribute(SessionKeys.USER_OBJECT);
	%>
		<tags:menu loggedInUser="<%=loggedInUser%>"></tags:menu>
		<%
	boolean registerSuccess = false;
	boolean register = request.getParameter("Password") !=null; 
	User userGuy = 	Database.getEmail(request.getParameter("Email"));
	boolean sameEmail = userGuy == null ;
	if(!sameEmail){
		sameEmail = userGuy.getUserName().equals( loggedInUser.getUserName());
	
	}
	if(loggedInUser != null)
	{
			
	
		if (register && sameEmail) {
			registerSuccess = Database.saveUser(new User(loggedInUser.getUserName(), 
									   request.getParameter("Password"), 
									   request.getParameter("Email"),
									   request.getParameter("FirstName"),
									   request.getParameter("LastName")));
	}
	
	
		
		if (registerSuccess) {
			loggedInUser.setEmail(request.getParameter("Email"));
			loggedInUser.setFirstName(request.getParameter("FirstName"));
			loggedInUser.setLastName(request.getParameter("LastName"));
			loggedInUser.setPassword(request.getParameter("Password"));
			%>
			<tags:message message="Changes registered!"></tags:message>
			<%
		}
		else
		{
			String blogOwnerName = StringHelpers.getProfileOwnerName(request.getRequestURL().toString());
			User profileUser = Database.getUser(blogOwnerName);
			if(loggedInUser != null && blogOwnerName.equals(loggedInUser.getUserName()))
			{
			%>
			<div class="ProfileBox">
				<form action="profile.jsp" method="get">
					<span>First Name:</span><input type="text" name="FirstName"
						value="<%=loggedInUser.getFirstName() %>"></input><br /> <span>Last
						Name:</span><input type="text" name="LastName"
						value="<%=loggedInUser.getLastName() %>"></input><br /> <span>
						Email address:</span><input type="text" name="Email"
						value="<%=loggedInUser.getEmail() %>"></input><br /> <span>
						User	Name: </span><span><%=profileUser.getUserName() %></span><br /> <span>Password:</span><input 
						type="password" name="Password"
						value="<%=loggedInUser.getPassword() %>"></input><br /> <input
						type="submit" value="Register"></input>
				</form>
			</div>
			<%
			}
			
			else if (profileUser != null)
			{
				%>
			<span>First Name: </span>
			<span><%=profileUser.getFirstName() %></span>
			<br />
			<span>Last Name: </span>
			<span><%=profileUser.getLastName() %></span>
			<br />
			<span>Email address: </span>
			<span><%=profileUser.getEmail() %></span>
			<br />
			<span>Username: </span>
			<span><%=profileUser.getUserName() %></span>
			<br />
			<a href="/blog/<%=profileUser.getUserName()%>">Go to his blog</a>
			<%
			} 
			else if(!register || !sameEmail){
				%>
				<tags:error errorMsg="Changes has not been registerd"></tags:error>
				<%
			}
			else
			{
				%>
			<tags:error errorMsg="This user doesn't exist or something!"></tags:error>
			
			
			
		<%}
		
			
		} 
}else {
	%>
	<tags:message message="You need to logg inn to view this!"></tags:message>
	<%
}
%>
</body>
</html>